package com.foreknow.service.impl;

import com.foreknow.dao.impl.AdminDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Admin;
import com.foreknow.service.AdminService;

import java.sql.Connection;
import java.sql.SQLException;

public class AdminServiceImpl implements AdminService {
    @Override
    public boolean register(Admin admin) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        AdminDaoImpl adminDao = new AdminDaoImpl();
        adminDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = adminDao.register(admin);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public Admin login(String username, String password) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        AdminDaoImpl adminDao = new AdminDaoImpl();
        adminDao.setConnection(conn);
        return  adminDao.login(username,password);
    }

    public static void main(String[] args) throws SQLException {
        AdminService service = new AdminServiceImpl();
//        Admin admin = new Admin();
//        admin.setUsername("xxx");
//        admin.setPassword("123456");
//        boolean isRight = service.register(admin);
//        System.out.println(isRight);

        Admin admin1 = service.login("xxx","123456");
        System.out.println(admin1.getUsername()+"---"+admin1.getPassword());
    }
}
